{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# <center> Time Series and Statistical Arbitrage HW 3 -- Volatility Forecasting </center>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "&copy; Kaiwen Zhou 2023"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Question 1\n",
    "\n",
    "You will estimate the parameters of a few GARCH-type models using SPY data (the\n",
    "S&P 500 ETF) from February 1, 1993 to Oct 20, 2023. I would like you to build this\n",
    "from scratch, rather than use a package.\n",
    "\n",
    "(a) Estimate the parameters of a GARCH(1,1) model. Compute the maximum value\n",
    "of the likelihood function.\n",
    "\n",
    "(b) Estimate a GARCH(1,1) model with conditional returns having a Generalized\n",
    "Error Distribution. Compute the maximum value of the likelihood function. Using\n",
    "the Schwartz Bayesian Criteria, does the higher likelihood compensate enough for\n",
    "the extra parameter?\n",
    "\n",
    "(c) Estimate a TGARCH(1,1) model with conditional returns having a normal dis-\n",
    "tribution. Compute the maximum value of the likelihood function. Using the\n",
    "Schwartz Bayesian Criteria again, does the higher likelihood compensate enough\n",
    "for the extra parameter compared to the GARCH(1,1) in part (a)?\n",
    "One way to download historical prices for SPY in Python is to pip install the yfinance\n",
    "library and download the data using\n",
    "\n",
    "Note: Use the ”Adj Close” column, which adjusts for dividends.\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "vscode": {
     "languageId": "plaintext"
    }
   },
   "outputs": [],
   "source": [
    "import yfinance as yf\n",
    "df = yf.download(\"SPY\", start=’1993-02-01’, end=’2023-10-21’)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Question 2\n",
    "\n",
    "One of the big trends in options this year is the dramatic rise of trading in zero-days-\n",
    "to-expiry (0DTE) options . These are options that expire within 24 hours. Options\n",
    "that expire every day of the week are now available for popular securities like SPY and\n",
    "QQQ. According to the CBOE, 44% of the S&P500 option volume is for options with\n",
    "less than 24 hours to expiration, for a notional amount of over $500bn traded per day.\n",
    "The figure below lists prices and implied volatilities for puts and calls on SPY options\n",
    "as of the close on Friday, October 20, 2023. The middle column are the strikes, and\n",
    "the data on the left side are for the call options, and the data on the right side are for\n",
    "the put options. The first five rows are for options that expire the following Monday,\n",
    "one trading day later."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img src=option_prices.png width=750, height=400>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Use the GARCH model in part (a) and the TGARCH model in part (c) above to\n",
    "price the 421 straddle (a straddle is a combination of a put and call at the same strike\n",
    "and expiration). You should price the straddle using Monte Carlo simulations over a\n",
    "one-day horizon. Explain how you get the volatility forecast from Friday to Monday.\n",
    "Notice that the market price of this straddle is 2.05 + 2.10 = 4.15. Do the models want\n",
    "to buy or sell the straddle? The TGARCH model should place a higher value on these\n",
    "options. Give a one sentence explanation why.\n",
    "As a pure bonus, you can try a GARCH model with a day-of-week dummy variable.]\n",
    "to potentially account for the possibility that volatility varies by day of the week. This\n",
    "is very tricky and you might not be able to get it to work. If you do try this and feel\n",
    "like you got it to work, feel free to email me your code.\n",
    "A few additional comments:\n",
    "\n",
    "• The SPY options stop trading at 4:15pm, whereas SPY stops trading at 4:00pm.\n",
    "On Friday, the closing price of SPY was 421.19 at 4:00 but went down to 421.08\n",
    "by 4:15 when the options closed. However, since we are pricing a straddle that is\n",
    "close to delta-neutral, adjusting the starting price of SPY will not affect the value\n",
    "of the straddle: the put will be worth a little more and the call will be worth a\n",
    "little less.\n",
    "\n",
    "• On that Friday, there were rumors that the Middle East fighting would escalate\n",
    "over the weekend. The market may have been pricing in some additional risk.\n",
    "Obviously, GARCH-type models can’t take these factors into account, since they\n",
    "just use historical returns.\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Question 3\n",
    "\n",
    "n class, I mentioned that Ordinary Least Squares (OLS) is a special case of GMM. In\n",
    "this exercise, you will verify that. Consider the linear regression model\n",
    "yi = α + βxi + ϵi\n",
    "where ϵi has a mean of zero and a variance of σ2. The sum of squared residual errors\n",
    "is\n",
    "SSE =\n",
    "NX\n",
    "i=1\n",
    "[yi − (α + βxi)]2\n",
    "and in OLS, we minimize the SSE by taking the partial derivatives of SSE with respect\n",
    "to α and β. Use those two equations to form moment conditions (in this case, M =\n",
    "P = 2). Simulate some data for a given α and β, and use GMM to estimate those two\n",
    "parameters. You can also verify your results by doing a linear regression:\n",
    "import statsmodels.api as sm\n",
    "X = sm.add_constant(x)\n",
    "model = sm.OLS(y,X)\n",
    "results = model.fit()\n",
    "results.params\n",
    "3\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Question 4\n",
    "\n",
    "n class, we talked about the paper “An Empirical Comparison of Alternative Models\n",
    "of the Short-Term Interest Rate” by Chan, Karolyi, Longstaff, and Sanders (CKLS),\n",
    "where they estimate parameters for eight popular models of short-term interest rates\n",
    "using GMM. In this exercise, you will replicate their study on one of those models.\n",
    "(a) Estimate the four parameters of the unrestricted model.\n",
    "(b) Estimate the two parameters in the Geometric Brownian Motion (GBM) model.\n",
    "Just like with the other excercises, I would like you to estimate the parameters from\n",
    "scratch, rather than using a package. I have attached one-month interest rate data\n",
    "from WRDS (the start date of 1964-06-30 and the end date of 1989-12-29 was chosen\n",
    "to match CKLS). I am also attaching the original CKLS paper. I was able to match\n",
    "the parameters of Table III of their paper relatively closely, but not exactly. In order\n",
    "to match the parameters, the data needs to be scaled correctly. First of all, you should\n",
    "divide the interest rates by 100, since 5% is represented as 5.0 and not 0.05. You should\n",
    "also include ∆t = 1/12 in the moment conditions. In other words, the first moment\n",
    "condition, for example, should be:\n",
    "E[rt+1 − rt − (α + βrt)∆t] = 0\n",
    "and the third moment condition should be:\n",
    "E[ϵ2\n",
    "t+1 − σ2r2γ\n",
    "t ∆t] = 0\n",
    "Also, to make things easier, you can choose to do two-step GMM rather than iterated\n",
    "GMM, and you can compute the weighting matrix assuming the data are uncorrelated\n",
    "so you don’t have to compute the Newey-West adjustment if you would like. You may\n",
    "have to vary the initial parameter guesses because sometimes the optimizer gets stuck\n",
    "in a local maximum that is not the global maximum.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
